package com.sms.servlet;


import com.sms.comment.SMSComment;
import com.sms.model.UserVO;
import com.sms.pojo.Role;
import com.sms.pojo.Student;
import com.sms.pojo.User;
import com.sms.service.RoleService;
import com.sms.service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("register")
    public String register(User user, Model model) {
        return "/register";
    }

    public String checkUserName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("userName");
        if (username == null || username.trim().length() == 0) {
            return null;
        }
        //验证用户是否存在
        User user = userService.queryUserByName(username);
        if (user != null) {
            response.getWriter().write("1");
        } else {
            response.getWriter().write("0");
        }
        return null;
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
//        //获取用户名和密码
//        String username = request.getParameter("userName");
//        String password = request.getParameter("password");
//
//        System.out.println(username + "---" + password);
//
//        if (StringUtils.isEmpty(username)) {
//            request.setAttribute("msg", "用户名不能为空");
//            return "/login.jsp";
//        }
//        if (StringUtils.isEmpty(password)) {
//            request.setAttribute("msg", "密码不能为空");
//            return "/login.jsp";
//        }
//        //验证用户名和密码是否正确
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//        Subject subject = SecurityUtils.getSubject();
//        token.setRememberMe(false);
//        String error = null;
//        try {
//            subject.login(token);
//        } catch (UnknownAccountException e) {
//            error = "用户名错误";
//        } catch (IncorrectCredentialsException e) {
//            e.printStackTrace();
//            error = "密码错误";
//        } catch (ExcessiveAttemptsException e) {
//            error = "登录失败多次";
//        } catch (AuthenticationException e) {
//            error = "其他错误请重试";
//        }
//        if (error != null) {
//            request.setAttribute("msg", error);
//            return "/login.jsp";
//        }
        return "index";
    }

    //退出功能
    public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception {

        return "redirect:/jsp/index.jsp";
    }

    public String userInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Role role = roleService.queryRoleByUserId(user.getUserId());
        UserVO userVO = new UserVO();
        if (SMSComment.STUDENT_ROLE.equals(role.getRoleName())) {
            Student student = userService.queryStudentUser(user.getUserId());
            userVO = userVO.getStudentUser(user, userService.queryStudentUser(user.getUserId()));
        } else if (SMSComment.TEACHER_ROLE.equals(role.getRoleName())) {
            userVO = userVO.getTeacherUser(user, userService.queryTeacherUser(user.getUserId()));
        } else {
            userVO = userVO.getUser(userService.queryUserByName(user.getUserName()));
        }
        request.setAttribute("userInfo", userVO);
        return "redirect:/jsp/userInfo.jsp";
    }
}
